//factory
import store from '@/store';
/**
 * 非负整数 >= 0
 * @param emptyMessage
 * @param message
 */
const createNonNegativeInteger = (
  emptyMessage = '请输入正整数', message = '值必须大于0'
) => (rule, value, callback) => {
  if (_.isUndefined(value)) callback();

  const _value = Number(value);
  if (!_.isInteger(_value)) {
    callback(new Error(emptyMessage));
  } else if (_value < 0) {
    callback(new Error(message));
  } else {
    callback();
  }
};

/**
 * 手机号码验证
 * @param emptyMessage
 * @param message
 * @param required
 * @return {Function}
 */
const createMobile = (required = true, emptyMessage = '手机号码不能为空', message = '手机号码格式不正确') =>
  (rule, value, callback) => {
    if (_.isUndefined(value) || _.isEmpty(value)) {
      if (required == false) {
        callback();
      } else {
        callback(new Error(emptyMessage));
      }
    }
    const telArea = store.getters.selectTelArea;
    if(telArea === '86' || telArea === '+86'){
      const reg = /^((1[3-9][0-9])+\d{8})$/;

      if (reg.test(value)) {
        callback();
      } else {
        callback(new Error(message));
      }
    }else{
      callback();
    }

  };

/**
 * 邮箱验证
 * @param emptyMessage
 * @param message
 */
const createEmail = ({required = true, emptyMessage = '', message = '邮箱格式不正确'}) => (rule, value, callback) => {
  var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;

  if (_.isUndefined(value) || _.isEmpty(value)) {
    if (required == false) {
      callback();
    } else {
      callback(new Error(emptyMessage));
    }
  }

  if (!reg.test(value)) {
    callback(new Error(message));
  } else {
    callback();
  }
};

/**
 * 合法uri验证
 */
const createUri = (message = 'url格式不正确') => (rule, value, callback) => {
  if (_.isEmpty(value)) {
    callback();
  } else {
    const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
    if (!urlregex.test(value)) {
      callback(new Error(message));
    } else {
      callback();
    }
  }
};

/**
 *
 * 银行卡格式
 */
const createCardNumber = (message = '银行卡号格式不正确') => (rule, value, callback) => {
  if (_.isEmpty(value)) {
    callback();
  } else {
    const pattern = /^([1-9]{1})(\d{15}|\d{18})$/;
    if (!pattern.test(value)) {
      callback(new Error(message));
    } else {
      callback();
    }
  }
};

/**
 * 正数 >= 0-100
 * @param emptyMessage
 * @param message
 */
const createInRangeFloat = (
  emptyMessage = '请输入数值', message = '数值范围0-100'
) => (rule, value, callback) => {
  if (_.isEmpty(value)) {
    callback();
  } else {
    if (!_.inRange(value, 100)) {
      callback(new Error(message));
    } else {
      callback();
    }
  }
};

const createDate = (
  message = '日期格式不正确'
) => (rule, value, callback) => {
  if (_.isEmpty(value)) {
    callback();
  } else {
    if (!moment(value).isValid()) {
      callback(new Error(message));
    } else {
      callback();
    }
  }
};

/**
 * 账户名或手机号
 * @returns {Function}
 */
const createUsernameOrMobile = (
  message = '请输入正确的账户名或手机号码'
) => (rule, value, callback) => {
  const reg = /^1[0-9]{10}$/;

  if (_.isEmpty(value)) {
    callback();
  } else {
    if (value.length < 6 || value.length > 16) {
      callback(new Error(message));
    } else {
      callback();
    }
  }
};

const createChCharacters = (
  message = '必须输入汉字'
) => (rule, value, callback) => {
  const reg = /^[\u4e00-\u9fa5]/g;

  if (_.isEmpty(value)) {
    callback();
  } else {
    if (!reg.test(value)) {
      callback(new Error(message));
    } else {
      callback();
    }
  }
};

/**
 * 最大汉字
 * @returns {Function}
 */
const createMaxChCharacters = (
  {
    length = 16,
    message = '长度必须小于16个字符'
  }
) => (rule, value, callback) => {
  const newValue = value.replace(/[\u4e00-\u9fa5]/g, '**');

  if (_.isEmpty(newValue)) {
    callback();
  } else {
    if (newValue.length > length) {
      callback(new Error(message));
    } else {
      callback();
    }
  }
};

export {
  createMobile,
  createNonNegativeInteger,
  createEmail,
  createDate,
  createUri,
  createCardNumber,
  createInRangeFloat,
  createUsernameOrMobile,
  createChCharacters,
  createMaxChCharacters,
};
